package com.wolfcode.flow.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletResponse;

import com.wolfcode.flow.domain.vo.BusBpmnInfoVo;
import org.apache.commons.io.IOUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.wolfcode.common.annotation.Log;
import com.wolfcode.common.core.controller.BaseController;
import com.wolfcode.common.core.domain.AjaxResult;
import com.wolfcode.common.enums.BusinessType;
import com.wolfcode.flow.domain.BusBpmnInfo;
import com.wolfcode.flow.service.IBusBpmnInfoService;
import com.wolfcode.common.utils.poi.ExcelUtil;
import com.wolfcode.common.core.page.TableDataInfo;


/**
 * 流程定义明细Controller
 *
 * @author wolfcode
 * @date 2022-11-28
 */
@RestController
@RequestMapping("/flow/info")
public class BusBpmnInfoController extends BaseController {
    @Autowired
    private IBusBpmnInfoService busBpmnInfoService;

    /**
     * 查询流程定义明细列表
     */
    @PreAuthorize("@ss.hasPermi('flow:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(BusBpmnInfo busBpmnInfo) {
        startPage();
        List<BusBpmnInfo> list = busBpmnInfoService.selectBusBpmnInfoList(busBpmnInfo);
        return getDataTable(list);
    }

    /**
     * 导出流程定义明细列表
     */
    @PreAuthorize("@ss.hasPermi('flow:info:export')")
    @Log(title = "流程定义明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BusBpmnInfo busBpmnInfo) {
        List<BusBpmnInfo> list = busBpmnInfoService.selectBusBpmnInfoList(busBpmnInfo);
        ExcelUtil<BusBpmnInfo> util = new ExcelUtil<BusBpmnInfo>(BusBpmnInfo.class);
        util.exportExcel(response, list, "流程定义明细数据");
    }

    /**
     * 获取流程定义明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('flow:info:query')")
    @GetMapping(value = "/{type}/{id}")
    public void getInfo(@PathVariable("type") String type,
                        @PathVariable("id") Long id,
                        HttpServletResponse response) throws IOException {
        InputStream is = busBpmnInfoService.getResource(type, id);
        // xml流还是图片流
        IOUtils.copy(is, response.getOutputStream());
    }

    /**
     * 新增流程定义明细
     */
    @PreAuthorize("@ss.hasPermi('flow:info:add')")
    @Log(title = "流程定义明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BusBpmnInfo busBpmnInfo) {
        return toAjax(busBpmnInfoService.insertBusBpmnInfo(busBpmnInfo));
    }

    /**
     * 修改流程定义明细
     */
    @PreAuthorize("@ss.hasPermi('flow:info:edit')")
    @Log(title = "流程定义明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BusBpmnInfo busBpmnInfo) {
        return toAjax(busBpmnInfoService.updateBusBpmnInfo(busBpmnInfo));
    }

    /**
     * 删除流程定义明细
     */
    @PreAuthorize("@ss.hasPermi('flow:info:remove')")
    @Log(title = "流程定义明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return toAjax(busBpmnInfoService.deleteBusBpmnInfoById(id));
    }

    /**
     * 部署流程定义
     */
    @PreAuthorize("@ss.hasPermi('flow:info:deploy')")
    @Log(title = "流程定义明细", businessType = BusinessType.INSERT)
    @PostMapping("/deploy")
    public void deploy(BusBpmnInfoVo busBpmnInfoVo) {
        busBpmnInfoService.deploy(busBpmnInfoVo);
    }


}
